package com.example.mobile2021606170hsy0406homework;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class Main6Activity extends AppCompatActivity {
    double y;
    private TextView tvMainTitle;
    private TextView tvMainEvaluation;
    private TextView tvMainAskfor;
    private TextView tvMainX;
    private EditText edtTxtMainX;
    private Button btMainResult;
    private EditText edtTxtMainResult;
    private ImageView ivMainFunction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        initView();
    }

    private void initView() {
        tvMainTitle = findViewById(R.id.tv_main_title);
        tvMainEvaluation = findViewById(R.id.tv_main_evaluation);
        tvMainAskfor = findViewById(R.id.tv_main_askfor);
        tvMainX = findViewById(R.id.tv_main_x);
        edtTxtMainX = findViewById(R.id.edtTxt_main_x);
        btMainResult = findViewById(R.id.bt_main_result);
        edtTxtMainResult = findViewById(R.id.edtTxt_main_result);

    }

    public void resultonClick(View view) {
        String a = edtTxtMainResult.getText().toString();
        if ((a.length() == 0)) {
            Toast.makeText(Main6Activity.this, "请输入x的值", Toast.LENGTH_SHORT).show();
        } else {
            double x = Double.parseDouble(a);
            if ((x < 1)) {
                y = x;
                edtTxtMainResult.setText(String.valueOf(y));
            } else if (1 <= x && x < 10) {
                y = 2 * x - 1;
                String str = String.format("%.2f", y);
                edtTxtMainResult.setText(String.valueOf(str));
            } else if (x >= 10) {
                y = 3 * x - 11;
                String str = String.format("%.2f", y);
                edtTxtMainResult.setText(String.valueOf(str));

            }
        }
    }
}
